//
//  FSCReplayVListView.swift
//  FamiliesSchoolConnection
//
//  Created by 花花蔡 on 2016/12/30.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

import UIKit

class FSCReplayVListView: UIView {

    @IBOutlet weak var likeListL:UILabel!
    @IBOutlet weak var likeLineV:UIView!
    
    //纪录当前已经添加的标签视图
    var replayListArray:[UILabel] = []
    
    //接收消息模型的属性
    var messageVM:FSCMessageViewModel? {
        
        didSet {
            
            //默认在添加回复列表视图的时候,需要先清除cell上面原来的标签
            for label in replayListArray {
                //1.从父视图中先移除
                label.removeFromSuperview()
            }
            //2.在清空数组
            replayListArray.removeAll()
            
            //获得点赞人数
            let likeNum = messageVM?.message.likenum ?? 0
            
            likeListL.attributedText = messageVM?.AttributeLikeStr
            //添加回复列表模型数组
          
            let listModels = messageVM?.replayListAttributeArr ?? []
            //判断当前点赞字符串是否有值
            if likeNum == 0 || listModels.count == 0 {
                likeLineV.isHidden = true
            } else {
                likeLineV.isHidden = false
            }
            
            for attributeStr in listModels {
                
                let l = UILabel(attributeTitle: attributeStr, fontSize: 12)
                l.frame = CGRect(x: 8, y: likeLineV.frame.maxY, width: 400, height: 44)
                l.textAlignment = .left
                addSubview(l)
                //同时添加到数组中记录
                replayListArray.append(l)
            }
            
            //使用三方约束视图
            for (i,label) in replayListArray.enumerated() {
                if i == 0 {
                    if likeNum == 0 {
                        label.snp.makeConstraints({ (make) in
                        make.top.equalTo(self.snp.top).offset(8)
                            make.left.equalTo(self.snp.left).offset(8)
                            make.right.equalTo(self.snp.right).offset(-8)
                        })
                    }else {
                    label.snp.makeConstraints({ (make) in
                        make.top.equalTo(likeLineV.snp.bottom).offset(8)
                        make.left.equalTo(self.snp.left).offset(8)
                        make.right.equalTo(self.snp.right).offset(-8)
                    })
                    }
                } else {
                    label.snp.makeConstraints({ (make) in
                    make.top.equalTo(replayListArray[i-1].snp.bottom).offset(5)
                    make.left.equalTo(self.snp.left).offset(8)
                    make.right.equalTo(self.snp.right).offset(-8)
                    })
                }
            }
        }
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
}
